/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.sedona.sql.datasources.osmpbf.iterators;

import static org.apache.sedona.sql.datasources.osmpbf.ParseUtils.dataInputStreamBlob;

import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.zip.DataFormatException;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.sedona.sql.datasources.osmpbf.build.Fileformat.Blob;
import org.apache.sedona.sql.datasources.osmpbf.build.Osmformat;
import org.apache.sedona.sql.datasources.osmpbf.extractors.DenseNodeExtractor;
import org.apache.sedona.sql.datasources.osmpbf.model.OSMEntity;

public class BlobIterator implements Iterator<OSMEntity> {
  Blob blob;
  Osmformat.PrimitiveBlock primitiveBlock;
  int primitiveGroupIdx;

  Iterator<OSMEntity> iterator;

  Osmformat.PrimitiveGroup currentPrimitiveGroup;

  public BlobIterator(Blob blob) throws DataFormatException, IOException {
    primitiveBlock = Osmformat.PrimitiveBlock.parseFrom(dataInputStreamBlob(blob));
    primitiveGroupIdx = 0;

    currentPrimitiveGroup = primitiveBlock.getPrimitivegroup(primitiveGroupIdx);
    iterator = resolveIterator();

    this.blob = blob;
  }

  @Override
  public boolean hasNext() {
    return primitiveGroupIdx < primitiveBlock.getPrimitivegroupList().size() - 1
        || iterator.hasNext();
  }

  @Override
  public OSMEntity next() {
    if (iterator.hasNext()) {
      return iterator.next();
    }

    primitiveGroupIdx += 1;

    currentPrimitiveGroup = primitiveBlock.getPrimitivegroup(primitiveGroupIdx);

    iterator = resolveIterator();

    return iterator.next();
  }

  Iterator<OSMEntity> resolveIterator() {
    return IteratorUtils.chainedIterator(
        new WayIterator(currentPrimitiveGroup.getWaysList(), primitiveBlock.getStringtable()),
        new RelationIterator(
            currentPrimitiveGroup.getRelationsList(), primitiveBlock.getStringtable()),
        new NodeIterator(currentPrimitiveGroup.getNodesList(), primitiveBlock),
        currentPrimitiveGroup.getDense() != null
            ? new DenseNodeIterator(
                currentPrimitiveGroup.getDense().getIdCount(),
                primitiveBlock.getStringtable(),
                new DenseNodeExtractor(
                    currentPrimitiveGroup.getDense(),
                    primitiveBlock.getLatOffset(),
                    primitiveBlock.getLonOffset(),
                    primitiveBlock.getGranularity()))
            : Collections.emptyIterator());
  }
}
